package de.msiggi.Sportsdata.Tools;

import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import de.msiggi.Sportsdata.Testfiles.MatchDateComparator;
import de.msiggi.Sportsdata.Webservices.Matchdata;
import de.msiggi.Sportsdata.Webservices.SportsdataSoap;

public class GenerateOutput {


	public List<String> generateOutputCompleteSeason(
			TreeMap<Integer, GameDay> gameDays) {
		Set<Integer> keysc = gameDays.keySet();
		DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy HH:mm ");
		
		StringBuffer sb = new StringBuffer();
		
		List<String> gameDayOut = new ArrayList<String>();
		
		for (Integer integer : keysc) {
			//sb.append("ID: " + integer + "\n");
			GameDay gd = gameDays.get(integer);
			List<Matchdata> machtes = gd.getMatches();
			Collections.sort(machtes, new MatchDateComparator());
			sb.append("----------------\n");
			sb.append(gd.getGamedayGroup() + "\n");
			sb.append("----------------\n");
			for (Matchdata matchdata : machtes) {
				String outStr = matchdata.getNameTeam1() + " - "
						+ matchdata.getNameTeam2();
				sb.append(formatter.format(matchdata.getMatchDateTime()
						.getTime()) + " : ");
				sb.append(String.format("%-45s", outStr));
				sb.append((matchdata.isMatchIsFinished() ? matchdata
						.getPointsTeam1() + ":" + matchdata.getPointsTeam2()
						: "_-_") + "\n");
			}
			gameDayOut.add(sb.toString());
			sb.setLength(0);
		}
		return gameDayOut;

	}
	
	public static void main(String[] args) {
		SportsdataSoap sds = LoadData.loadData();
		GenerateGameDays gen = new GenerateGameDays();
		
		try {
			TreeMap<Integer, GameDay> gameDays = gen
					.generateSeasonWithGameDaySplit(sds.getMatchdataByLeagueSaison("BL1", "2012"));
			GenerateOutput genOut = new GenerateOutput();
			List<String> output = genOut.generateOutputCompleteSeason(gameDays);
			for (String string : output) {
				System.out.println(string);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
	}

}
